import * as http from "http"

function getUrl(i: number): string {
    let prefix: String = "http://dy-public.oss-cn-shenzhen.aliyuncs.com/interviewTestData/"
    let suffix: String = ".txt"
    return prefix.toString() + i.toString() + suffix.toString()
}

function getNum(url: string): Promise<void> {
    // input your code 
    let promise = new Promise<void>(function(resolve, reject) {
        http.get(url)
          .end(function(err, res) {
              if (!err) {
                  resolve(res)
              } else {
                  reject(err)
              }
          })
    })
    return promise;
}

function regexpFromat(str: string):  RegExpMatchArray | null {
    // input your code
    let result: any = []
    let response =  asyncExecute()
    let promise = response.then(data => {
        let string = data.toString().replace(/\s/g, "")
        let res = string.match(new RegExp(str))
        return res
    })
    result.push(promise)
    return result
}

function syncExecute(): void {
    // input your code
    for (let num = 1; num < 11; num++) {
        let res = getNum(getUrl(num))
        res.then(data => {
            console.log(data)
        })
    }
}

async function asyncExecute(): Promise<number | string> {
   // input your code 
   let result: any = []
   for (let num = 1; num < 11; num++) {
       let res = getNum(getUrl(num))
       let promise = res.then(data => {
           return data
       })
       result.push(promise)
   }
   return result;
}


async function main(): Promise<number | string> {
    await console.log("------------------")
    await console.log("------------------")
    Promise.all([asyncExecute(), syncExecute(), regexpFromat('/\d+(\\.\\d+){0}/')]).then((data) => {
        console.log(data)
    })
    return;
}

main()

